-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
ENH: Added DataFrame.nsorted
to select top n
rows according to column-dependent order
#61457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
DataFrame.nsorted
to select top n
rows according to column-dependent orderDataFrame.nsorted
to select top n
rows according to column-dependent order
DataFrame.nsorted
to select top n
rows according to column-dependent orderDataFrame.nsorted
to select top n
rows according to column-dependent order
This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this. |
The PR has been waiting for reviewers since then. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Some minor comments. Can you also add this API docs in doc/source/reference/frame.rst
and series.rst
.
pandas/_typing.py
Outdated
# Arguments for nsorted, nsmallest and nlargest. | ||
NsmallestNlargestKeep = Literal["first", "last", "all"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove TypeAlias
? Also, can you keep the style here consistent by not ending with a period .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No reason, good catch! Note to myself: add the type after extracting a constant in Pycharm, because the IDE won't do it for me (so far).
pandas/core/frame.py
Outdated
This method is equivalent to | ||
``df.nsorted(n, columns, ascending=False)``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer if this line was removed; including nsorted
in the See Also
section below seems sufficient.
pandas/core/frame.py
Outdated
This method is equivalent to | ||
``df.nsorted(n, columns, ascending=True)``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
pandas/core/methods/selectn.py
Outdated
n = self.n | ||
dtype = self.obj.dtype | ||
if not self.is_valid_dtype_n_method(dtype): | ||
raise TypeError(f"Cannot use method '{method}' with dtype {dtype}") | ||
raise TypeError(f"Cannot use n-sorting with dtype {dtype}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is okay but wonder if Cannot sort dtype {dtype}
might be more clear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to agree with your point. I updated the message.
Series.nlargest | ||
Series.nsmallest | ||
Series.nsorted |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added nsorted and moved the sorting methods to the "Reshaping, sorting" section
""" | ||
Return the top `n` elements, sorted in ascending or descending order. | ||
|
||
Parameters | ||
---------- | ||
n : int, default 5 | ||
Return this many descending sorted values. | ||
ascending : bool | ||
If True, return the smallest `n` elements. If False, return | ||
largest `n` elements. | ||
keep : {'first', 'last', 'all'}, default 'first' | ||
When there are duplicate values that cannot all fit in a | ||
Series of `n` elements: | ||
|
||
- ``first`` : return the first `n` occurrences in order | ||
of appearance. | ||
- ``last`` : return the last `n` occurrences in reverse | ||
order of appearance. | ||
- ``all`` : keep all occurrences. This can result in a Series of | ||
size larger than `n`. | ||
|
||
Returns | ||
------- | ||
Series | ||
The `n` top values in the Series. | ||
|
||
See Also |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since nsorted got added to the series API docs, I believe it requires its proper docstring as well. There is a lot of redundancy with nlargest and nsmallest. Let me know if it should be done differently.
I applied the comments and the checks passed, so the PR should be ready for re-review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some small doc things I saw. Otherwise I'm OK if other maintainers are OK
Co-authored-by: Irv Lustig <[email protected]>
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.PR is ready for review.